package com.situ.car.filter;


import com.situ.car.pojo.entity.Admin;
import com.situ.car.pojo.vo.AdminVo;
import com.situ.car.pojo.vo.RoleVo;
import com.situ.car.service.IAdminService;
import com.situ.car.service.IPermissionService;
import com.situ.car.service.Impl.AdminServiceImpl;
import com.situ.car.service.Impl.PermissionServiceImpl;
import com.situ.car.util.CookieUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


@WebFilter(filterName = "login", urlPatterns = "/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("LoginFilter.init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("LoginFilter.doFilter");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String requestURI = request.getRequestURI();
        System.out.println("requestURI: " + requestURI);
        String method = request.getParameter("method");
        if (requestURI.startsWith("/static")
                || requestURI.equals("/verifyCode")
                || requestURI.equals("/page/login")
                || requestURI.equals("/page/register")
                || requestURI.equals("/page/forget")
                || requestURI.equals("/admin") && "login".equals(method)
                || requestURI.equals("/admin") && "register".equals(method)) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin == null) {
            Cookie cookie = CookieUtils.getCookie(request, "autoLogin");
            if (cookie != null) {
                //存在自动登录
                String[] values = cookie.getValue().split("&");
                String account = values[0];
                String password = values[1];
                IAdminService adminService = new AdminServiceImpl();
                admin = adminService.login(account, password);
                if (admin != null) {
                    session.setAttribute("admin", admin);
                    IPermissionService permissionService = new PermissionServiceImpl();
                    AdminVo adminVo = adminService.selectRole(account);
                    List<RoleVo> roleVos = permissionService.selectPath(adminVo.getRoleId());
                    session.setAttribute("roleVos", roleVos);
                    filterChain.doFilter(servletRequest, servletResponse);
                    return;
                }
            }
            response.sendRedirect("/page/login");
            return;
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("LoginFilter.destroy");
    }
}
